package com.by.service;

/**
 * @author LSIR
 * @version 0.1
 * @className OrderService
 * @date 2024/9/79:58
 * @since jdk11
 */
import com.by.dao.OrderItemDao;
import com.by.dao.OrderMasterDao;
import com.by.feignClients.StockServiceClient;
import com.by.models.OrderingDto;
import com.by.models.OrderingOk;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
@Slf4j
public class OrderService {

    @Autowired
    private OrderMasterDao orderMasterDao;
    @Autowired
    private OrderItemDao orderItemDao;

    @Autowired
    private StockServiceClient stockServiceClient;

    @Resource(name = "redisTemplate")
    private ValueOperations<String,String> valueOperations;

    //@Transactional
    @GlobalTransactional(name = "ordering")
    public OrderingOk createOrder(OrderingDto order) {

        valueOperations.set("orderId",order.getId().toString());

        //第一步扣库存
        stockServiceClient.decrease(order.getProducts());
        log.debug("扣库存好了");
        //第二部落库
        orderMasterDao.insert(order);
        //int i = 5/0;
        orderItemDao.insertBatch(order.getProducts());

        log.debug("创建订单成功");
        return   new  OrderingOk().setOrderId(order.getId()).setNeedPay(BigDecimal.valueOf(108.98));

    }
}
